// tcpsocket project tcpsocket.go
package tcpsocket

import (
	"com/mixmedia/utils/SyncMap"
	"sync"
)

const Version string = "1.01"

type TcpServer interface {
	Start(eventhandler EventHandler) (err error)
	Write(connkey string, content string)
	Close(connkey string)
	SetDebug(status bool)
}

type EventHandler interface {
	RegisterServer(server TcpServer)
	HandleConnect(connkey string)
	HandleReade(connkey string, content string)
	HandleWrite(connkey string, content string) (result string)
	HandleDisConnect(connkey string)
	HandleError(connkey string, err error)
}

func New(host, port string) (s TcpServer) {
	s = &tcpserver{
		readQuene:                  make(chan *BufferEvent),
		writeQuene:                 make(chan *BufferEvent),
		host:                       host,
		port:                       port,
		connection_maps:            SyncMap.NewTCPConn(),
		connection_write_chan_maps: NewChanBufferEvent(),
		debug: true,
		lock:  new(sync.Mutex),
	}

	return s
}
